﻿using Undercover.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Undercover.ViewModel
{
    public class UserLibraryViewModel : ViewModelBase
    {
        public UserLibraryViewModel() : this(new UserLibrary())
        {
        }

        public UserLibraryViewModel(UserLibrary userLibrary)
        {
            _userLibrary = userLibrary;
            _wordPairCollection = new ObservableCollection<WordPair>(userLibrary.WordPairs);

            EditCommand = new RelayCommand<WordPair>(
                (WordPair p) =>
                {
                    _selectedWordPair = p;
                    ServiceLocator.Current.GetInstance<INavigationService>()
                        .NavigateTo(new Uri("/View/WordPairPage.xaml", UriKind.Relative));
                }
            );

            RemoveCommand = new RelayCommand<WordPair>((WordPair p) => { _wordPairCollection.Remove(p); });
            ClearUseHistoryCommand = new RelayCommand(
                () =>
                {
                    foreach (var w in _userLibrary.WordPairs)
                    {
                        w.UseCount = 0;
                    }
                }
            );
        }

        public ICommand EditCommand { get; private set; }

        public ICommand RemoveCommand { get; private set; }

        public ICommand ClearUseHistoryCommand { get; private set; }

        public ObservableCollection<WordPair> WordPairCollection
        {
            get { return _wordPairCollection; }
        }
        private ObservableCollection<WordPair> _wordPairCollection = null;

        public WordPair SelectedWordPair
        {
            get { return _selectedWordPair; }
            private set
            {
                _selectedWordPair = value;
            }
        }
        private WordPair _selectedWordPair = null;

        private UserLibrary _userLibrary;
    }
}
